package pl.edu.pw.MidEmu.UI.Components;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Point;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;

import pl.edu.pw.MidEmu.Base.Components.BaseFrame;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Sensing.Sensors.ASensor;
import pl.edu.pw.MidEmu.Sensing.Sensors.DisplaySensor;
import pl.edu.pw.MidEmu.UI.Controllers.ShowSensorsController;
import pl.edu.pw.MidEmu.UI.Views.ShowSensorsView;

/**
 * Frame with GUI for managing sensor list
 * @author Mateusz Starodab
 *
 */
public class ShowSensorsFrame extends BaseFrame<JFrame, ShowSensorsController> implements ShowSensorsView
{
	private JButton btnOK;
	private DefaultListModel lstModel;
	private JList lstSensors;

	private JMenuItem miAddSensor;
	private JMenuItem miRemoveSensor;

	private JMenuItem miStartSensor;

	private JMenuItem miStopSensor;
	private JMenuItem miViewSensorData;
	private MidasComponent parent;
	private JPopupMenu pmContext;
	private JPanel pnlCenter;
	private JPanel pnlContent;

	private JPanel pnlSouth;
	private Repainter repainter;

	public ShowSensorsFrame(MidasComponent component)
	{
		ShowSensorsController controller = new ShowSensorsController();
		controller.setView(this);
		setController(controller);
		JFrame frame = new JFrame();
		parent = component;
		setComponent(frame);

		getComponent().setTitle(Consts.MainFrame_Title + " - " + parent.getName() + " - " + Consts.ShowSensors_Title);
		getComponent().setSize(400, 380);
		getComponent().setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		pnlContent = new JPanel();
		pnlContent.setLayout(new BorderLayout());

		pnlCenter = new JPanel();
		pnlCenter.setLayout(new BorderLayout());
		pnlCenter.setBorder(BorderFactory.createTitledBorder(Consts.ShowSensors_PanelCaption));

		lstModel = new DefaultListModel();
		rebindList();
		lstSensors = new JList(lstModel);
		lstSensors.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		lstSensors.addMouseListener(getController());

		pnlCenter.add(lstSensors, BorderLayout.CENTER);

		pnlContent.add(pnlCenter, BorderLayout.CENTER);

		pnlSouth = new JPanel();
		pnlSouth.setLayout(new FlowLayout(FlowLayout.CENTER));

		btnOK = new JButton(Consts.ShowSensors_OkButton);
		btnOK.setName(Consts.ShowSensors_OkButton);
		btnOK.addActionListener(getController());
		pnlSouth.add(btnOK);

		pnlContent.add(pnlSouth, BorderLayout.SOUTH);

		getComponent().setContentPane(pnlContent);

		pmContext = new JPopupMenu();
		miStartSensor = new JMenuItem(Consts.ShowSensors_StartSensorMenu);
		miStartSensor.setName(Consts.ShowSensors_StartSensorMenu);
		miStartSensor.addActionListener(getController());
		miStartSensor.setVisible(false);
		miStopSensor = new JMenuItem(Consts.ShowSensors_StopSensorMenu);
		miStopSensor.setName(Consts.ShowSensors_StopSensorMenu);
		miStopSensor.addActionListener(getController());
		miRemoveSensor = new JMenuItem(Consts.ShowSensors_RemoveSensorMenu);
		miRemoveSensor.setName(Consts.ShowSensors_RemoveSensorMenu);
		miRemoveSensor.addActionListener(getController());
		miViewSensorData = new JMenuItem(Consts.ShowSensors_ViewSensorData);
		miViewSensorData.setName(Consts.ShowSensors_ViewSensorData);
		miViewSensorData.addActionListener(getController());
		miAddSensor = new JMenuItem(Consts.ShowSensors_AddSensor);
		miAddSensor.setName(Consts.ShowSensors_AddSensor);
		miAddSensor.addActionListener(getController());

		pmContext.add(miStopSensor);
		pmContext.add(miStartSensor);
		pmContext.add(miViewSensorData);
		pmContext.add(miRemoveSensor);
		pmContext.addSeparator();
		pmContext.add(miAddSensor);
	}

	@Override
	public void dispose()
	{
		getComponent().setVisible(false);
		repainter.terminate();
		getComponent().dispose();
	}

	public void init()
	{
		repainter = new Repainter(getComponent());
		repainter.start();
	}

	@Override
	public void rebindList()
	{
		lstModel.clear();
		for (ASensor sensor : parent.getController().getSensors())
			lstModel.addElement(new DisplaySensor(sensor));
	}

	@Override
	public void removeSensor(ASensor selectedSensor)
	{
		parent.getController().removeSensor(selectedSensor);
		rebindList();
	}

	@Override
	public void showAddSensorWindow(Point point)
	{
		AddSensorFrame frame = new AddSensorFrame(parent);
		frame.getComponent().setLocation(point);
		frame.getComponent().setVisible(true);
		frame.getComponent().addWindowListener(getController());
	}

	@Override
	public void showPopupMenu(Component component, int x, int y, ASensor sensor)
	{
		if (sensor == null)
		{
			miStopSensor.setVisible(false);
			miStartSensor.setVisible(true);
			miStartSensor.setEnabled(false);
			miViewSensorData.setEnabled(false);
		}
		else
		{
			miStartSensor.setEnabled(true);
			miViewSensorData.setEnabled(true);
			if (sensor.isRunning())
			{
				miStopSensor.setVisible(true);
				miStartSensor.setVisible(false);
			}
			else
			{
				miStopSensor.setVisible(false);
				miStartSensor.setVisible(true);
			}
		}
		pmContext.show(component, x, y);
	}

	@Override
	public void showSensorDataWindow(ASensor selectedSensor, Point point)
	{
		GenericDataDisplayer dd = new GenericDataDisplayer(parent, Consts.ShowSensors_SensorData, selectedSensor.getSensorData());
		dd.getComponent().setLocation(point);
		dd.getComponent().setVisible(true);
	}
}
